#coding=utf-8


import urllib
import urllib2
import json


def send_request():
    # json文件的url地址,通过抓包获取. sort排序(近期热门/最新上映等) tags(全部类型,全部特色) start(电影起始值,姑且看为当前选项后的起始值) range(不晓得是啥)
    base_url = "https://movie.douban.com/j/new_search_subjects?sort=U&range=0,10&tags=%E7%94%B5%E5%BD%B1&"
    #请求报头
    headers = {
            "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko",
            "Accept": "application/json, text/plain, */*",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
            "Cookie": 'll="118282"; bid=r1-VF2zzRCU; _pk_ref.100001.4cf6=%5B%22%22%2C%22%22%2C1542115179%2C%22https%3A%2F%2Fwww.baidu.com%2Fs%3Fwd%3D%25E8%25B1%2586%25E7%2593%25A3%26rsv_spt%3D1%26rsv_iqid%3D0xd625055e00055717%26issp%3D1%26f%3D8%26rsv_bp%3D0%26rsv_idx%3D2%26ie%3Dutf-8%26tn%3Dbaiduhome_pg%26rsv_enter%3D1%26rsv_sug3%3D12%26rsv_sug1%3D10%26rsv_sug7%3D101%26rsv_t%3Dc677K5D15%252BiILQI%252F6pPh%252FmEESE1v7xsC7gapcoRBs2m7qYVf0niD9IX5LtbE4ncPdWBf%22%5D; ap_v=0,6.0; __utma=30149280.1598932635.1542115179.1542115179.1542115179.1; __utmc=30149280; __utmz=30149280.1542115179.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=223695111.189920279.1542115179.1542115179.1542115179.1; __utmc=223695111; __utmz=223695111.1542115179.1.1.utmcsr=baidu|utmccn=(organic)|utmcmd=organic|utmctr=%E8%B1%86%E7%93%A3; __yadk_uid=AFLc1mWLGY4Uk44PJ2LJPoEjhaA0iFbN; _vwo_uuid_v2=DBD541A1C6C459392829F97CCF1D2E9DB|cd2055e2b259cdfa04cc00ad40b33209; _pk_id.100001.4cf6=77303bb795bc0c8d.1542115179.1.1542116854.1542115179.'
            }

    start = 0
    i=1
    while True:
        #构建查询字符串
        query_dict = {"start": start, "limit": 1000}
        query_str = urllib.urlencode(query_dict)
        url = base_url + query_str

        request = urllib2.Request(url, headers=headers)

        response = urllib2.urlopen(request)
        #注意.read()方法只能获取一次,后续再次执行为空
        json_str = response.read()
        
        #json.loads()方法将json字符串转为对应的python数据类型
        if not json.loads(json_str):
            break
        
        print(i)
        i+=1

        #print(json_str)

        start += 20

if __name__ == "__main__":
    send_request()

